﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using STOCK.Model;
using STOCK.DAL;
using System.Data.SqlClient;

namespace STOCK
{
    /// <summary>
    /// FormulaUI.xaml 的交互逻辑
    /// </summary>
    public partial class FormulaUI : Window
    {
        public FormulaUI()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            List<string> whereList = new List<string>();
            List<SqlParameter> sqlparams = new List<SqlParameter>();
            if (cbCatId.IsChecked == true)
            {
                whereList.Add("CatId=@CatId");
                sqlparams.Add(new SqlParameter("@CatId", cmbCat.SelectedValue));
            }
            if (whereList.Count <= 0 || cmbCat.SelectedItem==null)
            {
                MessageBox.Show("没有选择任何搜索条件，请选择！");
                return;
            }
            else
            {
                string whereSql = string.Join(" and ", whereList);
                string sql = "select * from T_Formula"+" where "+whereSql;
                T_Formula[] items = new T_FormulaDAL().Search(sql, sqlparams);
                datagrid.ItemsSource = items;
            }
        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            FormulaEditUI win = new FormulaEditUI();
            win.IsAddNew = true;
            if (win.ShowDialog() == true)
            {
                LoadData();
            }

        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            FormulaEdit();
        }

        private void FormulaEdit()
        {
            FormulaEditUI win = new FormulaEditUI();
            T_Formula item = new T_Formula();
            item = (T_Formula)datagrid.SelectedItem;
            win.IsAddNew = false;
            win.EditingId = item.Id;
            if (win.ShowDialog() == true)
            {
                LoadData();
            }
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cmbCat.ItemsSource = new T_FormulaCategoryDAL().ListAll();
            colCat.ItemsSource = new T_FormulaCategoryDAL().ListAll();
            LoadData();
        }

        private void LoadData()
        {
            datagrid.ItemsSource = new T_FormulaDAL().ListAll();
        }

        private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            FormulaEdit();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            T_Formula item = (T_Formula)datagrid.SelectedItem;
            if (item == null)
            {
                MessageBox.Show("没有选择任何行！");
                return;
            }
            if (MessageBox.Show("真的要删除吗？", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                new T_FormulaDAL().DeleteById(item.Id);
                LoadData();
            }
        }

        private void btnListAll_Click(object sender, RoutedEventArgs e)
        {
            LoadData();
        }
    }
}
